package com.snack.manage.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.lamfire.wkit.ActionContext;
import com.lamfire.wkit.action.ActionForward;
import com.lamfire.wkit.action.ServletAction;
import com.snack.entity.User;

public abstract class AbstractAction extends ServletAction {

	protected int pageIndex = 1;
	protected int pageSize = 10;

	protected Map<String, Object> getSession() {
		return ActionContext.getActionContext().getSession();
	}

	protected Object getSessionAttr(String key) {
		return ActionContext.getActionContext().getSession().get(key);
	}

	protected void setSessionAttr(String key, Object value) {
		ActionContext.getActionContext().getSession().put(key, value);
	}

	protected void setRequestAttr(String key, Object value) {
		ActionContext.getActionContext().getRequest().put(key, value);
	}

	public User getCurrentUser() {
		return (User) getSessionAttr(SessionKeys.SESSION_USER);
	}
	
	public ActionForward showView(String url) {
		return forward("/WEB-INF/main" + url);
	}

	public String getServerPath() {
		HttpServletRequest request = ActionContext.getActionContext()
				.getHttpServletRequest();
		return getServerName() + request.getContextPath();
	}

	public String getServerName() {
		HttpServletRequest request = ActionContext.getActionContext()
				.getHttpServletRequest();
		int port = request.getServerPort();
		if (port == 80) {
			return "http://" + request.getServerName();
		}
		return "http://" + request.getServerName() + ":" + port;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

}
